using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Network;
using System;
namespace MicroMsg.Scene
{
	public class NetSceneSyncCheck
	{
		private const string TAG = "NetSceneSyncCheck";
		private static EventWatcher mEventWatcher;
		private static void onEventHandler(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs.mEventID == EventConst.ON_NET_MM_NET_NOOP)
			{
				NetSceneSyncCheck netSceneSyncCheck = new NetSceneSyncCheck();
				netSceneSyncCheck.doScene();
			}
		}
		private NetSceneSyncCheck()
		{
		}
		public static void initSyncCheck()
		{
			if (NetSceneSyncCheck.mEventWatcher != null)
			{
				return;
			}
			EventWatcher eventWatcher = new EventWatcher(null, null, new EventHandlerDelegate(NetSceneSyncCheck.onEventHandler));
			EventCenter.registerEventWatcher(EventConst.ON_NET_MM_NET_NOOP, eventWatcher);
		}
		private void doScene()
		{
			SessionPack sessionPack = new SessionPack();
			sessionPack.mCmdID = 25;
			sessionPack.mRequestObject = new object();
			sessionPack.mProcRequestToByteArray += new RequestToByteArrayDelegate(this.requestToByteArray);
			sessionPack.mCompleted += new SessionPackCompletedDelegate(this.onCompleted);
			Sender.getInstance().sendPack(sessionPack);
		}
		private byte[] requestToByteArray(object object_0)
		{
			int nUin = (int)AccountMgr.getCurAccount().nUin;
			byte[] mBytesSyncKeyBuf = NetSceneNewSync.mBytesSyncKeyBuf;
			if (nUin != 0 && mBytesSyncKeyBuf != null)
			{
				int num = mBytesSyncKeyBuf.Length;
				int value = ((nUin >> 13 & 524287) | num << 19) ^ 1442968193;
				int value2 = ((num >> 13 & 524287) | nUin << 19) ^ 1442968193;
				int num2 = 0;
				byte[] result = new byte[8 + num];
				Util.writeInt(value, ref result, ref num2);
				Util.writeInt(value2, ref result, ref num2);
				Util.writeBuffer(mBytesSyncKeyBuf, ref result, ref num2);
				return result;
			}
			Log.smethod_1("NetSceneSyncCheck", "new sync make pack body failed. ");
			return null;
		}
		private void onCompleted(object sender, PackEventArgs e)
		{
			SessionPack sessionPack = sender as SessionPack;
			byte[] mResponseBuffer = sessionPack.mResponseBuffer;
			if (e.isSuccess() && mResponseBuffer != null)
			{
				int num = 0;
				int num2 = Util.readInt(mResponseBuffer, ref num);
				Log.smethod_5("NetSceneSyncCheck", "new sync check success, continueFlag =  " + num2);
				if ((num2 & 7) != 0)
				{
					ServiceCenter.sceneNewSync.doScene(num2 & 7, syncScene.MM_NEWSYNC_SCENE_TIMER);
					return;
				}
			}
			else
			{
				Log.smethod_1("NetSceneSyncCheck", "new sync check failed. ");
			}
		}
	}
}
